//
//  SACommodityWarehousingHeaderView.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/29.
//

#import "SACommodityWarehousingHeaderView.h"
#import "SASalesOrderViewListModel.h"

@interface SACommodityWarehousingHeaderCell : UITableViewCell

@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) SASalesOrderViewListModel *model;
@property (nonatomic, copy) void (^addBlock)(void);

@end

@implementation SACommodityWarehousingHeaderCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

#pragma mark - event
-(void)clickedAddBtn {
    !self.addBlock ?: self.addBlock();
}

-(void)setModel:(SASalesOrderViewListModel *)model {
    _model = model;
    _contentLab.text = [NSString stringWithFormat:@"%@,%@",model.productCode,model.productName];
}

-(void)initUI {
    [self.contentView addSubview:self.contentLab];
    [self.contentView addSubview:self.addBtn];
}

-(UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:[UIImage imageNamed:@"commodity_add2"] forState:UIControlStateNormal];
        _addBtn.frame = SARectMake(0, 0, 42, 35);
        [_addBtn addTarget:self action:@selector(clickedAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

-(UILabel *)contentLab {
    if (!_contentLab) {
        _contentLab = [[UILabel alloc] initWithFrame:SARectMake(36, 0, 300, 35)];
        _contentLab.textColor = kBlackColor;
        _contentLab.text = @"H123，红色衣服";
        _contentLab.textAlignment = NSTextAlignmentLeft;
        _contentLab.font = kBFont(14);
    }
    return _contentLab;
}

@end

@interface SACommodityWarehousingHeaderView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SACommodityWarehousingHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASalesOrderViewListModel *)model {
    _model = model;
    [self.tableView reloadData];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SACommodityWarehousingHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SACommodityWarehousingHeaderCell" forIndexPath:indexPath];
    cell.model = self.model;
    WeakSelf
    cell.addBlock = ^{
        !weakSelf.addBlock ?: weakSelf.addBlock();
    };
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
  
-(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
                                                                         title:@"删除整款" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        !self.deleteBlock ?: self.deleteBlock();
    }];
    rowAction.backgroundColor = kPinkColor;
    NSArray *arr = @[rowAction];
    return arr;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(35);
}

#pragma mark - initUI
-(void)initUI {
    [self.contentView addSubview:self.tableView];
}

#pragma mark - lazy
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScaleNum(35)) style:UITableViewStylePlain];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.bounces = NO;
        [_tableView registerClass:[SACommodityWarehousingHeaderCell class] forCellReuseIdentifier:@"SACommodityWarehousingHeaderCell"];
    }
    return _tableView;
}

@end
